{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# imports"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%matplotlib inline\n",
    "import matplotlib.pyplot as plt\n",
    "from scipy.ndimage import correlate\n",
    "import numpy as np\n",
    "from skimage import data\n",
    "from skimage.color import rgb2gray\n",
    "from skimage.transform import rescale,resize"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# original image input"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "im = rgb2gray(data.coffee())\n",
    "im = resize(im, (64,64))\n",
    "print(im.shape)\n",
    "\n",
    "plt.axis('off')\n",
    "plt.imshow(im, cmap = 'gray');\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# horizontal edge filter"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "filter1 = np.array([\n",
    "    [ 1,  1,  1],\n",
    "    [ 0,  0,  0],\n",
    "    [-1, -1, -1]\n",
    "])\n",
    "\n",
    "new_image = np.zeros(im.shape)\n",
    "\n",
    "im_pad = np.pad(im, 1, 'constant')\n",
    "\n",
    "for i in range(im.shape[0]):\n",
    "    for j in range(im.shape[1]):\n",
    "        try:\n",
    "            new_image[i,j] = \\\n",
    "            im_pad[i-1,j-1] * filter1[0,0] + \\\n",
    "            im_pad[i-1,j] * filter1[0,1] + \\\n",
    "            im_pad[i-1,j+1] * filter1[0,2] + \\\n",
    "            im_pad[i,j-1] * filter1[1,0] + \\\n",
    "            im_pad[i,j] * filter1[1,1] + \\\n",
    "            im_pad[i,j+1] * filter1[1,2] +\\\n",
    "            im_pad[i+1,j-1] * filter1[2,0] + \\\n",
    "            im_pad[i+1,j] * filter1[2,1] + \\\n",
    "            im_pad[i+1,j+1] * filter1[2,2] \n",
    "        except:\n",
    "            pass\n",
    "\n",
    "plt.axis('off')\n",
    "plt.imshow(new_image, cmap='Greys');"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# vertical edge filter"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "filter2 = np.array([\n",
    "    [ -1,  0,  1],\n",
    "    [ -1,  0,  1],\n",
    "    [ -1,  0,  1]\n",
    "])\n",
    "\n",
    "new_image = np.zeros(im.shape)\n",
    "\n",
    "im_pad = np.pad(im,1, 'constant')\n",
    "\n",
    "for i in range(im.shape[0]):\n",
    "    for j in range(im.shape[1]):\n",
    "        try:\n",
    "            new_image[i,j] = \\\n",
    "            im_pad[i-1,j-1] * filter2[0,0] + \\\n",
    "            im_pad[i-1,j] * filter2[0,1] + \\\n",
    "            im_pad[i-1,j+1] * filter2[0,2] + \\\n",
    "            im_pad[i,j-1] * filter2[1,0] + \\\n",
    "            im_pad[i,j] * filter2[1,1] + \\\n",
    "            im_pad[i,j+1] * filter2[1,2] +\\\n",
    "            im_pad[i+1,j-1] * filter2[2,0] + \\\n",
    "            im_pad[i+1,j] * filter2[2,1] + \\\n",
    "            im_pad[i+1,j+1] * filter2[2,2] \n",
    "        except:\n",
    "            pass\n",
    "\n",
    "plt.axis('off')\n",
    "plt.imshow(new_image, cmap='Greys');"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# horizontal edge filter with stride 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "filter1 = np.array([\n",
    "    [ 1,  1,  1],\n",
    "    [ 0,  0,  0],\n",
    "    [-1, -1, -1]\n",
    "])\n",
    "\n",
    "stride = 2\n",
    "\n",
    "new_image = np.zeros((int(im.shape[0] / stride), int(im.shape[1] / stride)))\n",
    "\n",
    "im_pad = np.pad(im,1, 'constant')\n",
    "\n",
    "for i in range(0,im.shape[0],stride):\n",
    "    for j in range(0,im.shape[1],stride):\n",
    "        try:\n",
    "            new_image[int(i/stride),int(j/stride)] = \\\n",
    "            im_pad[i-1,j-1] * filter1[0,0] + \\\n",
    "            im_pad[i-1,j] * filter1[0,1] + \\\n",
    "            im_pad[i-1,j+1] * filter1[0,2] + \\\n",
    "            im_pad[i,j-1] * filter1[1,0] + \\\n",
    "            im_pad[i,j] * filter1[1,1] + \\\n",
    "            im_pad[i,j+1] * filter1[1,2] +\\\n",
    "            im_pad[i+1,j-1] * filter1[2,0] + \\\n",
    "            im_pad[i+1,j] * filter1[2,1] + \\\n",
    "            im_pad[i+1,j+1] * filter1[2,2] \n",
    "        except:\n",
    "            pass\n",
    "\n",
    "plt.axis('off')\n",
    "plt.imshow(new_image, cmap='Greys');"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# vertical edge filter with stride 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "filter2 = np.array([\n",
    "    [ -1,  0,  1],\n",
    "    [ -1,  0,  1],\n",
    "    [ -1,  0,  1]\n",
    "])\n",
    "\n",
    "stride = 2\n",
    "\n",
    "new_image = np.zeros((int(im.shape[0] / stride), int(im.shape[1] / stride)))\n",
    "\n",
    "im_pad = np.pad(im,1, 'constant')\n",
    "\n",
    "for i in range(0,im.shape[0],stride):\n",
    "    for j in range(0,im.shape[1],stride):\n",
    "        try:\n",
    "            new_image[int(i/stride),int(j/stride)] = \\\n",
    "            im_pad[i-1,j-1] * filter2[0,0] + \\\n",
    "            im_pad[i-1,j] * filter2[0,1] + \\\n",
    "            im_pad[i-1,j+1] * filter2[0,2] + \\\n",
    "            im_pad[i,j-1] * filter2[1,0] + \\\n",
    "            im_pad[i,j] * filter2[1,1] + \\\n",
    "            im_pad[i,j+1] * filter2[1,2] +\\\n",
    "            im_pad[i+1,j-1] * filter2[2,0] + \\\n",
    "            im_pad[i+1,j] * filter2[2,1] + \\\n",
    "            im_pad[i+1,j+1] * filter2[2,2] \n",
    "        except:\n",
    "            pass\n",
    "\n",
    "plt.axis('off')\n",
    "plt.imshow(new_image, cmap='Greys');"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "gdl_code",
   "language": "python",
   "name": "gdl_code"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
